<template>
  <div>
    <div class="edit-form-vue frame-page h-panel">
      <Form :label-width="110" :model="data" ref="form" :rules="rules" :showErrorTip="showErrorTip" 
      :validOnChange="false"
      labelPosition="left">        
        <FormItem label="名称" prop="title">
          <input type="text" v-model="data.title" />
        </FormItem>
<FormItem label="状态" prop="status">
          <h-switch :small="true" v-model="data.status" />
          </h-switch>
        </FormItem>
<FormItem label="排序" prop="sort">
          <NumberInput v-model="data.sort" :precision="0"></NumberInput>
        </FormItem>
<FormItem label="图片" prop="pic">
          <Oss :options="options" type="image" data-type="url" v-model="data.pic"></Oss>
        </FormItem>
<FormItem label="部门" prop="department_id">
          <Select v-model="data.department_id" :datas="departments" :filterable="true"></Select>
        </FormItem>
      </Form>
    </div>
    <footer class="h-modal-footer">   
      <Button class="h-btn" color="primary" @click="submit">确定</Button>  
      <Button class="h-btn" @click="close">关闭</Button>
    </footer>
  </div>
</template>
<script>
import oss from 'components/common/oss';

export default {
  props: {
    params: Object,
  },
  components: {
    Oss: oss
  },
  data() {
    return {
      required: true,
      showErrorTip: true,
      dict:[],
      departments:[],
      data: {
        
      },
      options: {
        filters: {
          mime_types: [
            { title: 'Image files', extensions: 'jpg,gif,png,jpeg,bmp' }
          ]
        }
      },
      rules: {required:['title','status','sort','pic','department_id']}
    };
  },
  watch: {
    required() {
      this.$refs.form.resetValid();
    }
  },
  mounted(){
    this.initDict();
    this.data = this.params;
  },
  methods: {
    initDict (){
      R.departments.dict().then(resp=>{
                  this.departments = resp.data;
                });
    },
    submit() {
      let validResult = this.$refs.form.valid();
      if (validResult.result) {
        let data = Object.assign({},this.data);
        if(data.tags){
          data.tags = data.tags.join(',');
        }        
        R.departmentJobs.store(data).then(resp => {
            if(resp.code==200){
              this.$Message.success(resp.message);
            }else{
              this.$Message.warn(resp.message);
            }     
            this.$emit('success');     
        });        
        this.close();
      }
    },
    close() {   
      this.$emit('close');
    },
    reset() {
      this.$refs.form.resetValid();
    }
  }
};
</script>